import 'package:flutter/material.dart';

/// Widget 列表扩展
/// 允许通过 [] 创建 Widget 列表，然后使用 .verticalShot() 或 .horizontalGallery() 转换为 Column 或 Row
extension WidgetListExtension on List<Widget> {
  /// 垂直拍摄（转换为 Column），支持链式调用设置属性
  Column verticalShot({
    Key? key,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection? textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down,
    TextBaseline? textBaseline,
    Widget? separator,
    double? spacing,
  }) {
    return Column(
      key: key,
      mainAxisAlignment: mainAxisAlignment,
      mainAxisSize: mainAxisSize,
      crossAxisAlignment: crossAxisAlignment,
      textDirection: textDirection,
      verticalDirection: verticalDirection,
      textBaseline: textBaseline,
      children:
          separator != null && spacing != null
              ? _addSeparator(separator, spacing)
              : this,
    );
  }

  /// 水平画廊（转换为 Row），支持链式调用设置属性
  Row horizontalGallery({
    Key? key,
    MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
    MainAxisSize mainAxisSize = MainAxisSize.max,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
    TextDirection? textDirection,
    VerticalDirection verticalDirection = VerticalDirection.down,
    TextBaseline? textBaseline,
    Widget? separator,
    double? spacing,
  }) {
    return Row(
      key: key,
      mainAxisAlignment: mainAxisAlignment,
      mainAxisSize: mainAxisSize,
      crossAxisAlignment: crossAxisAlignment,
      textDirection: textDirection,
      verticalDirection: verticalDirection,
      textBaseline: textBaseline,
      children:
          separator != null && spacing != null
              ? _addSeparator(separator, spacing)
              : this,
    );
  }

  /// 添加分隔符
  List<Widget> _addSeparator(Widget separator, double spacing) {
    if (isEmpty) return this;
    final List<Widget> result = [];
    for (int i = 0; i < length; i++) {
      result.add(this[i]);
      if (i < length - 1) {
        result.add(separator);
        if (spacing > 0) {
          result.add(SizedBox(width: spacing, height: spacing));
        }
      }
    }
    return result;
  }
}

/// Widget 扩展
/// 允许通过链式调用设置 Widget 的边框（如摄影中的装裱）
extension WidgetFramingExtension on Widget {
  /// 设置边框（支持所有方向，如摄影中的装裱）
  Widget frame({
    double? all,
    double? horizontal,
    double? vertical,
    double? top,
    double? bottom,
    double? left,
    double? right,
    EdgeInsets? edgeInsets,
  }) {
    if (edgeInsets != null) {
      return Padding(padding: edgeInsets, child: this);
    }

    if (all != null) {
      return Padding(padding: EdgeInsets.all(all), child: this);
    }

    if (horizontal != null || vertical != null) {
      return Padding(
        padding: EdgeInsets.symmetric(
          horizontal: horizontal ?? 0,
          vertical: vertical ?? 0,
        ),
        child: this,
      );
    }

    if (top != null || bottom != null || left != null || right != null) {
      return Padding(
        padding: EdgeInsets.only(
          top: top ?? 0,
          bottom: bottom ?? 0,
          left: left ?? 0,
          right: right ?? 0,
        ),
        child: this,
      );
    }

    return this;
  }

  /// 设置边框（仅水平方向）
  Widget frameHorizontal(double value) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: value),
      child: this,
    );
  }

  /// 设置边框（仅垂直方向）
  Widget frameVertical(double value) {
    return Padding(padding: EdgeInsets.symmetric(vertical: value), child: this);
  }

  /// 设置边框（所有方向）
  Widget frameAll(double value) {
    return Padding(padding: EdgeInsets.all(value), child: this);
  }
}
